package com.getpebble.android.common.version;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AndroidPebbleAppVersion {
    private int mMajor;
    private int mMinor;
    private int mPoint;

    private AndroidPebbleAppVersion(int major, int minor, int point) {
        this.mMajor = major;
        this.mMinor = minor;
        this.mPoint = point;
    }

    public int getMajor() {
        return this.mMajor;
    }

    public int getMinor() {
        return this.mMinor;
    }

    public int getPoint() {
        return this.mPoint;
    }

    public static AndroidPebbleAppVersion getVersionInfo() {
        String strVersion = "2.4.1";
        int majorVersion = 2;
        int minorVersion = 0;
        int pointVersion = 0;
        AndroidPebbleAppVersion fallbackAppVersion = new AndroidPebbleAppVersion(2, 0, 0);
        if (strVersion == null || strVersion.length() < 1) {
            return fallbackAppVersion;
        }
        if (strVersion.charAt(0) == 'v') {
            strVersion = strVersion.substring(1);
        }
        Matcher matcher = Pattern.compile("(\\d*)\\.(\\d*)\\.(\\d*)").matcher(strVersion);
        if (matcher.find() && matcher.groupCount() == 3) {
            majorVersion = Integer.parseInt(matcher.group(1));
            minorVersion = Integer.parseInt(matcher.group(2));
            pointVersion = Integer.parseInt(matcher.group(3));
        }
        return new AndroidPebbleAppVersion(majorVersion, minorVersion, pointVersion);
    }
}
